package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.assist.AssistBuild;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.BuildV2.State;
import com.xcity.pomelo.response.BuildV2Response.Invader;
import com.xcity.pomelo.response.EmployeeInfoResponse.EmployeeSimpleInfo;

import naga.x.game.time.TimeUpdater;

public class BuildV2Sync {

	public int id;
	public State state;
	public Invader invader;
	public List<com.xcity.pomelo.response.EmployeeInfoResponse.EmployeeSimpleInfo> employees = Collections.emptyList();
	public int takeMoneyElapseSec;
	public BuildV2Sync(BuildV2 building) {
		this.id = building.getId();
		this.state = building.getState();
		Player invader = building.currentInvader();
		Player owner = building.getVm().getOwner(); 
		if (invader != null) {
			this.invader = new Invader(invader);
			owner = invader;
		}
		long[] eids = building.currentEmployees();
		if (eids != null && eids.length > 0) {
			this.employees = new ArrayList<>(eids.length);
			for (long eid : eids) {
				Employee e;
				if (eid == 0L || (e = owner.getEmployees().get(eid)) == null) {
					this.employees.add(EmployeeSimpleInfo.EMPTY);
				} else {
					this.employees.add(new EmployeeSimpleInfo(e));
				}
			}
		}
		
		this.takeMoneyElapseSec = building.calcTakeMoneyElapseSecond();
		Player assistant = building.currentAssistant();
		if(assistant != null){
			AssistBuild assbuild = assistant.getAssists().findByOwnerId(owner.getId());
			if(assbuild != null){
				long endTime = building.getAssistTime() + BuildV2.BUILD_RETURN_MILLISECOND;
				long now = TimeUpdater.getInstance().now();
				this.takeMoneyElapseSec = (int)((now - assbuild.getLastStartMakeMoneyTime())/1000L);
				if(assbuild.getLastStartMakeMoneyTime() > endTime){
					this.takeMoneyElapseSec = -1;
				}
			}
		}
	}
	
}
